﻿using System;
using System.Windows.Forms;
using WorkFlowBLL;

namespace QLCF.Forms.admin
{
    public partial class frmListAdmin : Form
    {
        public int permission=0;
        public frmListAdmin()
        {
            InitializeComponent();
            permission = CtrAdmin.GetPermission(Program.UserEntity.Permission, Config.Enum.Function.frmListAdmin);
            if ((permission & (int)Config.Enum.Permission.Edit) != (int)Config.Enum.Permission.Edit)
            {
                btnEditAdmin.Enabled = false;
                btnSetPass.Enabled = false;
            }
            if ((permission & (int)Config.Enum.Permission.Add) != (int)Config.Enum.Permission.Add)
                btnAddAdmin.Enabled = false;
            if ((permission & (int)Config.Enum.Permission.Delete) != (int)Config.Enum.Permission.Delete)
                btnDeleteAdmin.Enabled = false;
            BindCbxRole();
            LoadAdmin(); 
        }        

        public void LoadAdmin()
        {
            grvAdmin.AutoGenerateColumns = false;
            CtrAdmin ctrA = new CtrAdmin();
            var admins = ctrA.GetListAdmin(txtUserName.Text,-1,Convert.ToInt32(cbxRole.SelectedValue),-1);
            grvAdmin.DataSource = admins;        
        }
        private void BindCbxRole()
        {
            cbxRole.Items.Clear();
            cbxRole.DisplayMember = "RoleName";
            cbxRole.ValueMember = "RoleID";
            CtrAdmin ctrA = new CtrAdmin();
            var items = ctrA.GetListRole().Items;
            DataContext.uspRoleGetByStatusResult role = new DataContext.uspRoleGetByStatusResult();
            role.RoleID = -1;
            role.RoleName = "Tất cả";
            items.Insert(0, role);
            cbxRole.DataSource = items;
        }       

        private void btnSearch_Click(object sender, EventArgs e)
        {
            LoadAdmin();
        }

        private void btnAddAdmin_Click(object sender, EventArgs e)
        {
            popupAddAdmin popa = new popupAddAdmin(this);
            popa.ShowDialog();
        }

        private void btnEditAdmin_Click(object sender, EventArgs e)
        {
            if (grvAdmin.SelectedRows.Count <= 0)
                return;
            int adminId = Convert.ToInt32(grvAdmin.SelectedRows[0].Cells["AdminID"].Value);
            popupEditAdmin popa = new popupEditAdmin(this, adminId);
            popa.ShowDialog();
        }

        private void btnDeleteAdmin_Click(object sender, EventArgs e)
        {
            if (grvAdmin.SelectedRows.Count <= 0)
                return;
            int adminId = Convert.ToInt32(grvAdmin.SelectedRows[0].Cells["AdminID"].Value);
            string uName=grvAdmin.SelectedRows[0].Cells["UserName"].ToString();
            CtrAdmin ctra = new CtrAdmin();
            if (ctra.DeleteAdmin(adminId) > 0)
            {
                ctra.LogInsert(Program.UserEntity.UserID,Program.UserEntity.UserName, 3, "Xóa người dùng \"" + uName + "\"","Quản lý người dùng");
                LoadAdmin();
            }
        }

        private void grvAdmin_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            btnEditAdmin.PerformClick();
        }

        private void btnSetPass_Click(object sender, EventArgs e)
        {
            if (grvAdmin.SelectedRows.Count <= 0)
                return;
            int adminId = Convert.ToInt32(grvAdmin.SelectedRows[0].Cells["AdminID"].Value);
            popupSetPassAdmin popa = new popupSetPassAdmin(adminId);
            popa.ShowDialog();
        }
    }
}
